package homelesspartners.server;

import homelesspartners.client.model.City;

import java.io.IOException;
import java.util.ArrayList;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ashbyutils.email.Email;

public class ContactServlet extends HttpServlet {

	public ContactServlet() {
	}

	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		doPost(arg0, arg1);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		try {
			String name = request.getParameter("theName");
			String emailAddress = request.getParameter("theEmail");
			String city = request.getParameter("theCity");
			String subject = request.getParameter("theSubject");
			String details = request.getParameter("theDetails");

			if (name == null || emailAddress == null || city == null || subject == null || details == null
					|| name.length() == 0 || name.length() == 0 || emailAddress.length() == 0 || city.length() == 0
					|| subject.length() == 0 || details.length() == 0) {
				String message =
						"<font color='red'>ERROR</font><br />Please enter all fields before submitting the form.";
				request.setAttribute("message", message);
				RequestDispatcher rd = request.getRequestDispatcher("contact.jsp");
				rd.forward(request, response);
				return;
			}

			System.out.println("[contact]");

			Email email = Email.createInstance();
			email.setTo("homelesspartners@gmail.com");
			email.setFrom(emailAddress);
			email.setSubject("[CONTACT FORM:" + city + "] " + subject);
			StringBuffer buffer = new StringBuffer();
			buffer.append("Message from: ");
			buffer.append(name);
			buffer.append(" (");
			buffer.append(emailAddress);
			buffer.append(")<br /><br />");
			buffer.append(details);
			email.setBody(buffer.toString());
			email.setHtml(true);

			email.send();

		} catch (AddressException ae) {
			ae.printStackTrace();
			String message =
					"<font color='red'>ERROR</font><br />The email address you entered appears to be invalid, please try again. If you require assistance, please contact homelesspartners@gmail.com.";
			request.setAttribute("message", message);
			RequestDispatcher rd = request.getRequestDispatcher("contact.jsp");
			rd.forward(request, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			String message =
					"<font color='red'>ERROR</font><br />An unknown error has occured, please contact homelesspartners@gmail.com for assistance";
			request.setAttribute("message", message);
			RequestDispatcher rd = request.getRequestDispatcher("contact.jsp");
			rd.forward(request, response);
			return;
		}

		String message =
				"Your form has been submitted, someone will get back to you as soon as possible. <br />(please note: we do not actively monitor the inbox from February to September, so it may take longer to get a response during that time. Thank you for your patience.";
		request.setAttribute("message", message);
		RequestDispatcher rd = request.getRequestDispatcher("contact.jsp");
		rd.forward(request, response);

	}

	public void init() throws ServletException {
		super.init();
		Data data = new Data();
		ArrayList cities = null;
		String[] cityNames = null;

		try {
			cities = data.getCities();
		} catch (DataException e) {
			e.printStackTrace();
		}
		if (cities != null) {
			cityNames = new String[cities.size()];
			for (int x = 0; x < cityNames.length; x++) {
				cityNames[x] = ((City) cities.get(x)).getName();
			}
		}
		getServletContext().setAttribute("cities", cityNames);
	}

}
